import tushare as ts
import numpy as np
import pandas as pd
import time
#检查factor 表 与 stock表 数据不相同的原因
ts.set_token('883c1e2da92c0f68194fe2c4b4aa0e4da880b41ef62ff556f2da00e7')
pro = ts.pro_api()


df_data = pd.read_hdf('stock_data.h5', key='data')
df_data.reset_index(inplace =True)
df_data = df_data.set_index([df_data['ts_code'],df_data['trade_date']],drop = False)
#df_data = df_data.swaplevel('trade_date', 'ts_code').sort_index(level = 0)
df_data.drop(['trade_date','ts_code'], 1, inplace =True)
start = time.clock()
df_data['open'] = df_data.apply(lambda x: round(x['open']*x['factor'],2), axis=1)
df_data['high'] = df_data.apply(lambda x: round(x['high']*x['factor'], 2),axis=1)
df_data['low'] = df_data.apply(lambda x: round(x['low']*x['factor'],2), axis=1)
df_data['close'] = df_data.apply(lambda x: round(x['close']*x['factor'],2), axis=1)


df_data.drop(['pre_close','change','pct_chg'],1,inplace= True)
end = time.clock()
print(df_data)
print('程序耗时：',end - start)
h5 = pd.HDFStore('stock_data_qfq.h5','w',complevel= 4,complib = 'blosc' )
h5['data'] = df_data
h5.close()